 import {
     createRouter,
     createWebHashHistory
 } from 'vue-router';
 import Index from '@/pages/index.vue'
 import NotFond from '@/pages/404.vue'
 import Login from '@/pages/login.vue'
 import Admin from '@/layouts/admin.vue'
 import Goodlist from '@/pages/goods/goodlist.vue'
 import Userlist from '@/pages/user/userList.vue'
 import Couponlist from '@/pages/coupon/couponList.vue'
 import Noticelist from '@/pages/notice/noticeList.vue'
 import Settinglist from '@/pages/setting/settingList.vue'
 import Tickslist from '@/pages/ticks/tickList.vue'
 import Commentlist from '@/pages/comment/commentList.vue'
 import Picturelist from '@/pages/picture/pictureList.vue'
 import Managerlist from '@/pages/manager/managerList.vue'
 

export const router = createRouter({
     history: createWebHashHistory(),
     routes: [{
             path: "/",
             name: "admin",
             component: Admin,
         },
         {
             path: "/login",
             component: Login,
             meta: {
                 title: "登录"
             }
         },
         {
             path: "/:pathMatch(.*)",
             component: NotFond,
             meta: {}
         },
     ],
 });

 const asyncrouters = [{
     path: "/",
     name:"/",
         component: Index,
         meta: {
             title: "后台首页"
         }
     }, {
         path: "/goods/list",
         name: "/goods/list",
         component: Goodlist,
         meta: {
             title: "商品列表"
         }
     },
      {
         path: "/user/list",
         name: "/user/list",
         component: Userlist,
         meta: {
             title: "用户列表"
         }
     },
      {
         path: "/order/list",
         name: "/order/list",
         component:Tickslist,
         meta: {
             title: "订单列表"
         }
     },
      {
         path: "/comment/list",
         name: "/comment/list",
         component:Commentlist,
         meta: {
             title: "评价列表"
         }
     },
      {
         path: "/image/list",
         name: "/image/list",
         component:Picturelist,
         meta: {
             title: "照片列表"
         }
     },
      
      {
         path: "/notice/list",
         name: "/notice/list",
         component:Noticelist,
         meta: {
             title: "公告列表"
         }
     },
      {
         path: "/setting/list",
         name: "/setting/list",
         component:Settinglist,
         meta: {
             title: "设置列表"
         }
     },
      {
         path: "/coupon/list",
         name: "/coupon/list",
         component: Couponlist,
         meta: {
             title: "优惠券列表"
         }
     },
      {
         path: "/manager/list",
         name: "/manager/list",
         component:Managerlist,
         meta: {
             title: "管理员管理"
         }
     },
      
      
      
     
]
 

export function addRouters(menus)
{
    let hasNewRouters = false;
    const addRoutersByMenus = (arr)=>
    {
       arr.forEach(e => {
           let item = asyncrouters.find(o => o.path === e.frontpath);
           if (item && !router.hasRoute(item.path))
           {
               //添加路由
               router.addRoute("admin", item);
               hasNewRouters = true;
           }

           if (e.child && e.child.length > 0)
           {
               addRoutersByMenus(e.child);    
           }
       });
    }

    addRoutersByMenus(menus);
    console.log(router.getRoutes());
    return hasNewRouters;
}
